package com.xhwl.centiotdata.suppliersystem.guangtuo.alarmhost.websocket;

import com.xhwl.centiotdata.pojo.constant.GuangTuoAlarmHostConstant;
import com.xhwl.centiotdata.utils.ThreadPoolUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhouyongzhou
 * @date 2023/04/28  13:55
 */
@Slf4j
@Service
public class AlarmHostDeviceWebsocketClientService {

    @Value("${guangTuo.alarmhost.wsIpAndPort:-1}")
    private String wsIpAndPort;


    @PostConstruct
    public void start() {
        try {
            if (StringUtils.isEmpty(wsIpAndPort) || wsIpAndPort.equals("-1")) {
                log.info("start to receive alarm-host device wsIpAndPort is null");
                return;
            }
            log.info("start to receive alarm-host device data");
            URI uri = new URI(wsIpAndPort.concat(GuangTuoAlarmHostConstant.WS_URL));
            Map<String, String> httpHeaders = new HashMap<>(4);
            httpHeaders.put("Origin", "http://" + uri.getHost());
            AlarmHostDeviceWebsocketClient deviceWebsocketClient = new AlarmHostDeviceWebsocketClient(uri, httpHeaders, ThreadPoolUtils.alarmExecutor);
            deviceWebsocketClient.connect();
        } catch (Exception e) {
            log.error("start to receive  alarm-host device data failed", e);
        }
    }
}
